from turtle import *
import turtle
import time

turtle.screensize(800, 600, "light blue")
playrr = turtle.Turtle()
screen = turtle.Screen()
playrr.shape('turtle')
playrr.color('yellow')

playrr.hideturtle()
playrr.penup()
playrr.speed(0)
playrr.setheading(270)
playrr.goto(-20,120)
playrr.showturtle()
class Maze(Turtle):
    size = 20

    def __init__(self, maze_list):

        Turtle.__init__(self)
        self.maze_list = maze_list

        self.hideturtle()
        self.speed(0)
        self.draw_walls()


    def draw_wall(self):
        self.pendown()
        self.begin_fill()

        self.fillcolor('red')

        for i in range(4):
            self.forward(self.size)
            self.right(90)
        self.end_fill()
        self.penup()

    def draw_walls(self):
        self.penup()

        self.goto(-130, 130)

        for row in range(13):
            for col in range(13):

                if self.maze_list[row][col] == 1:
                    self.draw_wall()

                self.goto(self.size * (col + 1) - 130, 130 - self.size * row)

            self.goto(-130, 130 - self.size * (row + 1))

m = 0
n = 5
def up():
    global m,n
    if n >= 0 and m >= 0:
        if maze_list[m-1][n] == 0:
            playrr.setheading(90)
            playrr.forward(20)
            m -= 1
        print(m,n)
    if m == 12 and n == 7:
        text = turtle.Turtle()
        text.hideturtle()
        text.penup()
        text.goto(-125, -10)
        text.color('blue')
        text.write('you win!', font=('SimHei', 48, 'bold'))

def down():
    global m, n
    if n >= 0 and m >= 0:
        if maze_list[m+1][n] == 0:
            playrr.setheading(270)
            playrr.forward(20)
            m += 1
        print(m,n)
    if m == 12 and n == 7:
        text = turtle.Turtle()
        text.hideturtle()
        text.penup()
        text.goto(-125, -10)
        text.color('blue')
        text.write('you win!', font=('SimHei', 48, 'bold'))
def left():
    global m, n
    if n >= 0 and m >= 0:
        if maze_list[m][n-1] == 0:
            playrr.setheading(180)
            playrr.forward(20)
            n -= 1
        print(m,n)
    if m == 12 and n == 7:
        text = turtle.Turtle()
        text.hideturtle()
        text.penup()
        text.goto(-125, -10)
        text.color('blue')
        text.write('you win!', font=('SimHei', 48, 'bold'))
def right():
    global m, n
    if n >= 0 and m >= 0:
        if maze_list[m][n+1] == 0:
            playrr.setheading(0)
            playrr.forward(20)
            n += 1
        print(m,n)
    if m == 12 and n == 7:
        text = turtle.Turtle()
        text.hideturtle()
        text.penup()
        text.goto(-125, -10)
        text.color('blue')
        text.write('you win!', font=('SimHei', 48, 'bold'))

maze_list = [
  [1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1],
  [1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
  [1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1],
  [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
  [1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1],
  [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  [1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1],
  [1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1],
  [1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1],
  [1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1],
  [1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1],
  [1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1],
  [1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1]
]

screen.onkey(up, 'Up')
screen.onkey(down, 'Down')
screen.onkey(left, 'Left')
screen.onkey(right, 'Right')
screen.listen()
while True:
    Maze(maze_list)
    time.sleep(0.01)